记一次chatgpt 客户端 bug导致我电脑进程达到系统上限
最近我的macbook终端执行任何命令都一直报错 类似 fork: Resource temporarily unavailable
用了很多方法试了, 知道有个程序在不断产生僵尸进程, 但是又不知道怎么解决
最后重启了也没解决
于是我把最后交给了 kiro 的Claude 模型帮我解决
帮我查询本机报错 fork: Resource temporarily unavailable 的原因以及解决
下面是llm的诊断过程
● Thought for 1s...
╰ The "fork: Resource temporarily unavailable" error suggests the system is hitting process or thread limits, so I need to check what the current
constraints are and how much is actually being used.
这个错误通常是进程/线程数达到系统限制导致的。我来检查一下本机的实际状态。
● Shell ulimit -a
● Shell echo "=== kern.maxproc ==="; sysctl kern.maxproc kern.maxprocperuid; echo "=== 当前进程总数 ==="; ps -ax | wc -l; echo "=== 当前用户进程数 ===";
ps -u "$(whoami)" | wc -l; echo "=== 线程数 ==="; sysctl kern.num_threads kern.num_taskthreads 2>/dev/null
● Thought for 1s...
╰ I've found the issue—the user has 2662 processes running under their account, and both the kernel limit `kern.maxprocperuid` and the ulimit for max
user processes are set to 2666, so they're essentially at capacity. Now I need to investigate which processes are consuming the most resources.
找到问题了。你的本机每用户进程数已经逼近系统上限:
- kern.maxprocperuid(每个用户最大进程数)= 2666
- 当前你的用户已经运行了 2662 个进程
只差 4 个就到顶了,所以 fork() 无法再创建新进程,报 Resource temporarily unavailable。
我再看看是什么在大量占用进程,这样才能对症解决:
● Shell ps -u "$(whoami)" -o comm | sed 's#.*/##' | sort | uniq -c | sort -rn | head -20
● Thought for 1s...
╰ Ah, so there are over 2,300 zombie processes hogging the per-user process limit — these are already dead but haven't been reaped by their parent
process. I need to track down which parent is responsible and either terminate it or restart it to clean them up.
真正的元凶找到了:2363 个 <defunct> 进程,也就是僵尸进程(zombie process)。
这些僵尸进程占满了你的每用户进程配额(2363 / 2666),导致几乎无法再 fork 新进程。僵尸进程本身已经死亡、不占
CPU/内存,但仍占用一个进程表项(PID)。它们无法被直接 kill,必须由父进程回收(wait)。所以关键是找到那个不回收子进程的父进程。
● Shell ps -axo ppid,stat,comm | awk '$2 ~ /Z/ {print $1}' | sort | uniq -c | sort -rn | head
● Thought for 1s...
╰ All 2363 zombies share the same parent process ID of 518, so I need to identify what that process is.
所有 2363 个僵尸进程的父进程都是同一个 PID 518。看看它是什么:
● Shell ps -p 518 -o pid,ppid,user,lstart,%cpu,%mem,stat,command; echo "---- 完整命令行 ----"; ps -p 518 -o command=
PID PPID USER STARTED %CPU %MEM STAT COMMAND
518 1 stephen Tue Aug 11 14:55:15 2026 0.6 0.7 S /Applications/ChatGPT.app/Contents/MacOS/ChatGPT
---- 完整命令行 ----
/Applications/ChatGPT.app/Contents/MacOS/ChatGPT
● Thought for 2s...
╰ The ChatGPT desktop app is spawning child processes without properly reaping them, creating zombie processes that consume system resources. The fix
is straightforward — restarting the app will cause the parent process to terminate, which triggers the init process to clean up all the orphaned
children and free those process slots.
根本原因已经完全定位清楚了。
诊断结论
┌─────────────────────────────────────┬───────────────────────┐
│ 项目 │ 数值 │
├─────────────────────────────────────┼───────────────────────┤
│ 每用户最大进程数 kern.maxprocperuid │ 2666 │
├─────────────────────────────────────┼───────────────────────┤
│ 你的用户当前进程数 │ 2662(逼近上限) │
├─────────────────────────────────────┼───────────────────────┤
│ 其中僵尸进程 <defunct> │ 2363 个 │
├─────────────────────────────────────┼───────────────────────┤
│ 僵尸进程的父进程 │ PID 518 = ChatGPT.app │
└─────────────────────────────────────┴───────────────────────┘
ChatGPT 桌面应用(/Applications/ChatGPT.app,启动于 14:55)在不断创建子进程后没有回收(wait),累积了 2363 个僵尸进程,吃光了进程配额。于是系统里任何程序调用
fork()(包括开新终端、运行命令)都会失败并报 fork: Resource temporarily unavailable。
僵尸进程不能被 kill 杀掉(它们已经死了),只能靠父进程回收,或者让父进程退出——父进程一死,这些僵尸会被 init(PID 1)接管并立即清理。
解决方法
立即解决(推荐):退出并重启 ChatGPT
# 优雅退出 ChatGPT(僵尸子进程会被 PID 1 回收,进程数立刻恢复)
osascript -e 'quit app "ChatGPT"'
# 如果优雅退出无效,强制结束
kill 518 # 或 kill -9 518
退出后再确认僵尸已清空:
ps -u "$(whoami)" -o stat | grep -c Z
应该会从 2363 降到接近 0,fork 错误随即消失。之后可以重新打开 ChatGPT 继续用。
后续建议
1. 这是 ChatGPT 桌面版的进程泄漏 bug——建议把它升级到最新版本,新版通常修复了这类子进程回收问题。若升级后仍复发,可以定期(如每天)重启该应用,或改用网页版。
2. 临时提高进程上限(治标不治本,仅作应急,重启失效):
sudo sysctl -w kern.maxproc=6000 kern.maxprocperuid=4000
3. 想监控是否复发,随时可跑:
ps -axo ppid,stat | awk '$2 ~ /Z/' | wc -l
要我直接帮你执行退出 ChatGPT 的命令来清理僵尸进程吗?这会关闭你的 ChatGPT 窗口(可能丢失未保存的对话状态),所以我先征求你同意。
▸ Credits: 1.21 • Time: 59s
花了半分钟, 1.21 个credit 查出来 是 ChatGPT 客户端的bug, 我随机关闭 ChatGPT 客户端 并更新 ChatGPT 客户端, 问题根除, 不得不佩服llm 检查bug的实力